package cn.hl.sfzy.service.impl;

import cn.hl.sfzy.entity.Question;
import cn.hl.sfzy.entity.Reply;
import cn.hl.sfzy.entity.User;
import cn.hl.sfzy.mapper.QuestionMapper;
import cn.hl.sfzy.mapper.ReplyMapper;
import cn.hl.sfzy.mapper.UserMapper;
import cn.hl.sfzy.service.ReplyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ReplyServiceImpl
 * @Description TODO
 * @Author yangshen
 * @Date 2024年10月26日 14:38
 * @Version 1.0
 */
@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Reply> find(Reply reply) {
        List<Reply> replies = replyMapper.find(reply);
        for (Reply reply1 : replies) {
            long questionId = reply1.getQuestion_id();
            Question question = questionMapper.selectById(questionId);
            reply1.setQuestion(question);
            long userId = reply1.getUser_id();
            User user = userMapper.selectById(userId);
            reply1.setUser(user);
        }
        return replies;
    }

    @Override
    public int add(Reply reply) {
        return replyMapper.add(reply);
    }

    @Override
    public int delete(long id) {
        return replyMapper.delete(id);
    }

    @Override
    public int update(Reply reply) {
        return replyMapper.update(reply);
    }

    @Override
    public List<Reply> findByQuestionId(Long id) {
        List<Reply> replies = replyMapper.selectByQuestionId(id);
        for (Reply reply1 : replies) {
            long questionId = reply1.getQuestion_id();
            Question question = questionMapper.selectById(questionId);
            reply1.setQuestion(question);
            long userId = reply1.getUser_id();
            User user = userMapper.selectById(userId);
            reply1.setUser(user);
        }
        return replies;
    }
}
